<?php

function resizeimage($image,$w,$h,$file=false)
{
    $info=getimagesize($image);
    $ow=$info[0];
    $oh=$info[1];
    $mime=$info['mime'];
    $mime=explode('/',$mime);
    $type=$mime[1];
    if($type=='vnd.wap.wbmp'){$type='xbmp';}
    $createfunc='imagecreatefrom'.$type;
    $imagefunc='image'.$type;
    if(!function_exists($createfunc) || !function_exists($imagefunc))
    {
        die("Not support the file format");
    }
    $op=$ow/$oh;
    $p=$w/$h;
    if($p>=$op)
    {
        $w=$op*$h;
    }else{
        $h=$w/$op;
    }
    $thumb = imagecreatetruecolor($w, $h);
    $source = $createfunc($image);
    imagesavealpha($source,true);
    imagealphablending($thumb,false);
    imagesavealpha($thumb,true);
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $w, $h, $ow, $oh);
    if($file)
    {
        $imagefunc($thumb,$file);
    }else{
        header('Content-type: image/'.$type);
        $imagefunc($thumb);
    }
    imagedestroy($source);
    imagedestroy($thumb);
}

function GrabImage($url,$filename="") 
{
    if($url=="") 
    {
        return false;
    }

    if($filename=="") 
    {
        $ext=strrchr($url,".");
        if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") 
        {
            return false;
        }
        $filename=date("YmdHis").$ext;
    }

    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $size = strlen($img);
    

    $fp2=@fopen($filename, "a");
    fwrite($fp2,$img);
    fclose($fp2);

    return $filename;
}

$url = "http://img3.cache.netease.com/www/logo/logo_png.png";
echo $url;
$file = GrabImage($url);
$img = imagecreatefrompng($file);
resizeImage($file, 500, 200, $file);

$url = "http://img3.fengniao.com/forum/attachpics/182/195/7278866_800.jpg";
echo $url;
$file = GrabImage($url);
$img = imagecreatefrompng($file);
resizeImage($file, 500, 200, $file);

?>
